<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/2/2 18:29
 * Remark:
 */

namespace App\Models\Auth;

use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;

class AuthBasicModel extends BaseModel
{
    protected $table = TableConsts::CY_AUTH_AUTH_TABLE_NAME;

    protected $primaryKey = TableConsts::CY_AUTH_AUTH_TABLE_PRIMARY_KEY;

    protected $fillable = [
        'auth_id',
        'company_id',
        'auth_name',
        'auth_type',
        'menu_id',
    ];

    public function isExistByCondition($conditionData){
        return $this->withoutGlobalScopes()
            ->firstOrnew($conditionData)->exists;
    }


    /**
     * 通过菜单ID获取权限组
     * @param $menuIds
     * @param string[] $select
     * @return mixed
     */
    public function getAuthSelectByMenuIdsAndAuthType($menuIds, $authType, array $select = ["*"]): mixed
    {
        return $this->select($select)
            ->whereIn('menu_id',$menuIds)
            ->where('auth_type',$authType)
            ->get();
    }

    /**
     * 获取权限列表
     * @return mixed
     */
    public function getAuthList(): mixed
    {
        return $this->get();
    }

    /**
     * 根据菜单获取权限
     * @param $menuId
     * @param $authType
     * @param string[] $select
     * @return mixed
     */
    public function getAuthSelectByMenuId($menuId,$authType,$select = ["*"]): mixed
    {
        return $this->select($select)
            ->where('menu_id',$menuId)
            ->where('auth_type',$authType)
            ->get();
    }

    public function getAuthByCompanyId(mixed $company_id,$select = ["*"])
    {
        return $this
            ->withoutGlobalScopes()
            ->where('company_id',$company_id)
            ->get();
    }


}